package exercise2;

import java.util.Stack;

public class Sort {

    /**
     * O(N^2)  稳定
     * @param array
     */
    public void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (;j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    public void shellSort(int[] array) {
        int gap = array.length - 1;
        while (gap > 0) {
            gap /= 2;
            shell(array,gap);
        }
    }

    private void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }


    /**
     * O(N^2)  不稳定
     * @param array
     */

    public void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);

        }
    }

    private void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public void selectSort2(int [] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = right;
            for (int i = left; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,minIndex,left);
            if (maxIndex == left) {
                maxIndex = minIndex;
            }
            swap(array,maxIndex,right);
            left++; right--;
        }
    }


    public void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end > 0) {
            swap(array,0,end);
            end--;
            siftDown(array,0,end);
        }
    }

    private void createBigHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            siftDown(array,parent,array.length - 1);
        }
    }

    private void siftDown(int[] array,int parent, int end) {
        int child = 2 * parent + 1;
        while (child <= end ) {
            if (child + 1 <= end && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,child,parent);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }

    }


    public void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int flag = 0;
            for (int j = 0; j < array.length - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array,j,j+1);
                    flag = 1;
                }
            }
            if (flag == 0) return;
        }
    }

    public void quickSort(int[] array) {
        quick(array,0,array.length - 1);
    }

    private void quick(int[] array,int start, int end) {
        if (start >= end) {
            return ;
        }

        if (end - start < 17) {
            insertSort(array,start,end);
            return ;
        }

        int midIndex = findMid(array,start,end);
        swap(array,midIndex,start);
        int divide = partition1(array,start,end);

        quick(array,start,divide - 1);
        quick(array,divide + 1,end);
    }

    private void insertSort(int[] array, int left, int right) {
        for (int i = left + 1 ; i <= right ; i++) {
            int tmp = array[i];
            int j = i - 1;
            for (; j >= left; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    private int findMid(int[] array,int left, int right) {
        int midIndex = left + (right - left) / 2;
        if (array[left] > array[right]) {
            if (array[midIndex] > array[left]) return left;
            else if (array[midIndex] < array[right]) return right;
            else return midIndex;
        } else {
            if (array[midIndex] > array[right]) return right;
            else if (array[midIndex] < array[left]) return left;
            else return midIndex;
        }
    }

    // Hoare法
    private int partition1(int[] array, int left, int right) {
        int i = left;
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            while (left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }

    private int partition2(int[] array, int left, int right) {
        int tmp = array[left];
        while (left < right) {
            while (left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while (left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    public void quickSortNor(int[] array) {
        Stack<Integer> stack = new Stack<>();
        int divide = partition1(array,0,array.length - 1);
        int left = 0;
        int right = array.length - 1;
        if (divide - 1 > left) {
            stack.push(left);
            stack.push(divide - 1);
        }
        if (divide + 1 < right) {
            stack.push(divide + 1);
            stack.push(right);
        }

        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            divide = partition1(array,left,right);
            if (divide - 1 > left) {
                stack.push(left);
                stack.push(divide - 1);
            }
            if (divide + 1 < right) {
                stack.push(divide + 1);
                stack.push(right);
            }
        }

    }

    /**
     * O(N*logN)    稳定
     * @param array
     */

    public void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length - 1);
    }

    private void mergeSortFun(int[] array, int left, int right) {
        if (left >= right) return;
        int midIndex = left + (right - left) / 2;
        mergeSortFun(array,left,midIndex);
        mergeSortFun(array,midIndex + 1,right);
        // 排序合并
        merge(array,left,right,midIndex);
    }

    private void merge(int[] array,int left, int right, int midIndex) {
        int[] tmpArray = new int[right - left + 1];
        int rightStart = midIndex + 1;
        int i = 0;
        int leftStart = left;
        while (leftStart <= midIndex && rightStart <= right) {
            if (array[leftStart] < array[rightStart]) {
                tmpArray[i++] = array[leftStart++];
            } else {
                tmpArray[i++] = array[rightStart++];
            }
        }
        while (leftStart <= midIndex) {
            tmpArray[i++] = array[leftStart++];
        }
        while (rightStart <= right) {
            tmpArray[i++] = array[rightStart++];
        }
        for (int j = 0 ; j < tmpArray.length; j++) {
            array[left + j] = tmpArray[j];
        }
    }
}
